﻿namespace WPFDBBrowser.Settings
{
    using System.Collections.Generic;
    using System.IO;
    using System.Xml.Serialization;
    using System.ComponentModel;

    public class ComponentsSettings : SettingsBase<List<ComponentInfo>>
    {
        public ComponentsSettings()
            : base(Path.Combine(
                Directory.GetCurrentDirectory(),
                SettingsFileNames.COMPONENTS_FILE))
        {
            this.ComponentInfos = new List<ComponentInfo>();
        }

        [XmlElement("Components")]
        public List<ComponentInfo> ComponentInfos { get; set; }

        public override bool SaveSettings()
        {
            base.Save(ComponentInfos);
            return true;
        }

        public override bool LoadSettings()
        {
            object obj = base.Load();
            if (obj == null)
                return false;
            this.ComponentInfos = (List<ComponentInfo>)obj;
            return true;
        }
    }

    [XmlRoot("Component")]
    public class ComponentInfo
    {
        public ComponentInfo()
            : this(null)
        {
        }

        public ComponentInfo(string assemblyName)
        {
            this.AssemblyName = assemblyName;
            this.Interfaces = new List<ImplementedInterface>();
        }

        [XmlAttribute("assembly")]
        public string AssemblyName { get; set; }
        [XmlElement("interfaces")]
        public List<ImplementedInterface> Interfaces { get; set; }

        public override string ToString()
        {
            return AssemblyName;
        }
    }
    [XmlRoot("Interface")]
    public class ImplementedInterface : INotifyPropertyChanged
    {
        public ImplementedInterface()
        {
        }
        public ImplementedInterface(BorwserInterfaces implementedInterface)
            : this(implementedInterface, null)
        {
        }
        public ImplementedInterface(BorwserInterfaces implementedInterface, string implementationClass)
        {
            this.Interface = implementedInterface;
            this.ClassName = implementationClass;
        }
        [Category("Main")]
        [XmlAttribute("interface")]
        public BorwserInterfaces _interface;
        public BorwserInterfaces Interface
        {
            get
            {
                return this._interface;
            }
            set
            {
                if (!this._interface.Equals(value))
                {
                    this._interface = value;
                    this.OnPropertytChanged("Interface");
                    this.OnPropertytChanged("InterfaceStr");
                }
            }
        }

        public string InterfaceStr
        {
            get
            {
                return this._interface.ToString();
            }
            set
            {
            }
        }

        [Category("Main")]
        [XmlAttribute("class")]
        public string ClassName { get; set; }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertytChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }

    public enum BorwserInterfaces
    {
        IDBFactory,
        IDBTreeExtension
    }
}
